package com.dam.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dam.common.Result;
import com.dam.entity.Algorithm;
import com.dam.entity.vo.AlgorithmDisplay;
import com.dam.entity.vo.AlgorithmQuery;
import com.dam.service.AlgorithmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dam
 * @since 2021-11-03
 */
@RestController
@RequestMapping("algorithm/cbyalgorithm7002")
@Api(tags = "算法管理")
public class AlgorithmController {

    @Autowired
    private AlgorithmService algorithmService;

    @GetMapping("/queryAllAlgorithm")
    @ApiOperation("查询所有算法")
    private Result queryAllAlgorithm() {
        List<AlgorithmDisplay> algorithmDisplayList = algorithmService.queryAlgorithmDisplayList();
        return Result.ok().data("algorithmDisplayList", algorithmDisplayList);
    }

    @GetMapping("/queryAlgorithmByType/{alType}")
    @ApiOperation("根据管类型查询算法")
    private Result queryAlgorithmByType(@PathVariable String alType) {

        QueryWrapper<Algorithm> wrapper = new QueryWrapper<>();
        wrapper.eq("type_id", alType);

        List<Algorithm> algorithmList = algorithmService.list(wrapper);
        return Result.ok().data("algorithmDisplayList", algorithmList);
    }

    @ApiOperation("不带条件的分页查询")
    @GetMapping("pageAlgorithm/{current}/{limit}")
    public Result pageAlgorithm(@PathVariable long current,
                                @PathVariable long limit) {
        //创建page对象
        Page<Algorithm> pageAlgorithm = new Page<>(current, limit);

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        algorithmService.page(pageAlgorithm, null);

        //总记录数
        long total = pageAlgorithm.getTotal();
        //数据list集合
        List<Algorithm> records = pageAlgorithm.getRecords();

        //方式二
        return Result.ok().data("total", total).data("rows", records);
    }

    //4 条件查询带分页的方法
    @ApiOperation("条件查询再分页")
    @PostMapping("pageAlgorithmCondition/{current}/{limit}")
    public Result pageAlgorithmCondition(@PathVariable long current, @PathVariable long limit,
                                         @RequestBody AlgorithmQuery algorithmQuery) {
        //创建page对象
        Page<AlgorithmDisplay> pageAlgorithm = new Page<>(current, limit);

        //构建条件
        QueryWrapper<Algorithm> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql 使用MybatisPlus之后无须再使用sql拼接
        String algorithmName = algorithmQuery.getName();
        String algorithmType = algorithmQuery.getTypeId();

        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(algorithmName)) {
            //构建条件
            wrapper.like("name", algorithmName);
        }
        if (!StringUtils.isEmpty(algorithmType)) {
            //构建条件
            wrapper.like("type_id", algorithmType);
        }

        wrapper.orderByDesc("insert_time");

        //调用方法实现条件查询分页
//        algorithmService.page(pageAlgorithm, wrapper);
        algorithmService.queryAlgorithmDisplayListWithWrapper(pageAlgorithm, wrapper);

        long total = pageAlgorithm.getTotal();//总记录数
        List<AlgorithmDisplay> records = pageAlgorithm.getRecords(); //数据list集合
        return Result.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("根据ID删除算法")
    @DeleteMapping("{id}")
    public Result removeById(@PathVariable String id) {
        boolean flag = algorithmService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @ApiOperation("添加算法")
    @PostMapping("addAlgorithm")
    public Result addAlgorithm(@RequestBody Algorithm algorithm) {
        boolean save = algorithmService.save(algorithm);
        if (save) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    //根据讲师id进行查询
    @ApiOperation("根据ID查询算法")
    @GetMapping("getAlgorithm/{id}")
    public Result getAlgorithm(@PathVariable String id) {
        Algorithm algorithm = algorithmService.getById(id);
        return Result.ok().data("algorithm", algorithm);
    }

    @ApiOperation("修改算法信息")
    @PostMapping("updateAlgorithm")
    public Result updateAlgorithm(@RequestBody Algorithm algorithm) {
        boolean flag = algorithmService.updateById(algorithm);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

}
